﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Ext.Net;
using WEB.Exceptions;
using System.Web.Security;
using WEB.BLL;


namespace WEB.Core.Controls
{
    public class BaseControl : UserControl
    {
        private Util.Permission _Permission = new Util.Permission();

        #region
        public Util.Permission Permission
        {
            get { return _Permission; }
            set { _Permission = value; }
        }
        public bool IsView
        {
            get { return _Permission.IsView; }
            set { _Permission.IsView = value; }
        }
        public bool IsAdd
        {
            get { return _Permission.IsAdd; }
            set { _Permission.IsAdd = value; }
        }
        public bool IsEdit
        {
            get { return _Permission.IsEdit; }
            set { _Permission.IsEdit = value; }
        }
        public bool IsDelete
        {
            get { return _Permission.IsDelete; }
            set { _Permission.IsDelete = value; }
        }
        public bool IsPrint
        {
            get { return _Permission.IsPrint; }
            set { _Permission.IsPrint = value; }
        }
        public bool IsImport
        {
            get { return _Permission.IsImport; }
            set { _Permission.IsImport = value; }
        }
        public bool IsExport
        {
            get { return _Permission.IsExport; }
            set { _Permission.IsExport = value; }
        }

        public WEB.BLL.Entities.UserEntity CurrentUser
        {
            get { return WEB.BLL.WEBContext.Current.User; }
        }
        #endregion

        public BaseControl()
        {
            if (WEBContext.Current.User == null)
            {
                FormsAuthentication.SignOut();
                FormsAuthentication.RedirectToLoginPage();
                return;
            }
        }

        #region
        protected virtual void CheckPer(bool Edit)
        {
            var _btnSave = (Component)FindControl("btnSave");
            if (_btnSave != null)
            {
                _btnSave.Hidden = !(IsEdit || (IsAdd && !Edit));
            }
            var _btnNew = (Component)FindControl("btnNew");
            if (_btnNew != null)
            {
                _btnNew.Hidden = !(IsAdd && Edit);
            }
        }

        public string GetMessage(object key)
        {
            return Util.Message.Get(key);
        }
        #endregion

        #region Notification
        public void Notification(Exception ex)
        {
            if (ex.InnerException is DBException)
                this.Notification((DBException)ex.InnerException);
            else
            {
                this.Notification("Notice", ex.Message, 0);
            }
        }
        
        public void Notification(DBException ex)
        {
            //string message = "";
            if (ex.Number > 0)
            {
                this.Notification("Notice", GetMessage(ex.Number), string.Empty);
            }

        }
        public void Notification(string message)
        {
            Notification("Notice", message, 0);
        }
        public void Notification(string title, string message, int number)
        {
            if (number == 0)
            {
                Ext.Net.Notification.Show(new NotificationConfig
                    {
                        Title = title,
                        ShowPin = true,
                        Html = message
                    });
            }
        }
        public void Notification(string title, string message, string detail)
        {
            if (string.IsNullOrEmpty(detail))
            {
                Ext.Net.Notification.Show(new NotificationConfig
                {
                    Title = title,
                    ShowPin = true,
                    Html = message
                });
            }
            else
            {
                Ext.Net.Notification.Show(new NotificationConfig
                {
                    Title = title,
                    ShowPin = true,
                    Tools = new ToolsCollection { new Tool { Type = ToolType.Help, Handler = X.MessageBox.Alert("Chi tiết", detail).ToScript() } },
                    Html = message
                });
            }
        }
        #endregion
    }
}
